//
//  ClubDetailViewController.swift
//  yuejian
//
//  Created by gyy on 2017/10/26.
//  Copyright © 2017年 gyy. All rights reserved.
//

import UIKit

class ClubDetailViewController: UITableViewController {

    var infoDict:NSDictionary!
//    var ClasBtn:UIButton!
//    var VideoBtn:UIButton!
    var videoPlayer:VideoPlayer!
    var hideState = false
    let WIDTH = UIScreen.main.bounds.width
    let HEIGHT = UIScreen.main.bounds.height

    override func viewDidLoad() {
        super.viewDidLoad()
        viewConfig()
    }
    
    func viewConfig(){
        view.backgroundColor = UIColor.white
        
        let navBar = CustomNavBar(frame: CGRect(origin: .zero, size: CGSize(width: WIDTH, height: 64)))
        navBar.initialize(text: nil)
        view.addSubview(navBar)
        
        let scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height-64))
        //scrollView.contentInsetAdjustmentBehavior = .never
        scrollView.contentSize = CGSize(width: 0, height: 364+(UIScreen.main.bounds.height-69)/3)
        scrollView.backgroundColor = view.backgroundColor
        scrollView.showsVerticalScrollIndicator = false
        view.addSubview(scrollView)
        
        let imageView = UIImageView()
        let pURL = "http://172.24.10.175/workout/Uploads/"
        imageView.setImageWith(URL(string: pURL+(infoDict.value(forKey: "thumb") as! String)),
                               placeholderImage: UIImage(named:"placeholder"))
        imageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: (UIScreen.main.bounds.height-69)/3)
        scrollView.addSubview(imageView)
       
        let label = UILabel(frame: CGRect(x: 15, y: imageView.bounds.height+10, width: UIScreen.main.bounds.width-30, height: 40))
        label.text = "简介"
        label.textAlignment = .center
        label.layer.cornerRadius = 5
        label.layer.borderColor = UIColor.lightGray.cgColor
        label.layer.borderWidth = 1
        scrollView.addSubview(label)
        
        let textView = UITextView(frame: CGRect(x: 15, y: label.frame.origin.y+50, width: UIScreen.main.bounds.width-30, height: 140))
        textView.text = infoDict.value(forKey: "introduction") as! String
        textView.isEditable = false
        textView.font = UIFont.systemFont(ofSize: 16)
        textView.layer.borderWidth = 1
        textView.layer.borderColor = UIColor.lightGray.cgColor
        scrollView.addSubview(textView)
        
        let equipsuggestLabel = UILabel(frame: CGRect(x: 15, y: textView.frame.origin.y+55, width: UIScreen.main.bounds.width-30, height: 20))
       equipsuggestLabel.text = "装备: \(infoDict.value(forKey: "equipsuggest") as! String)"
       equipsuggestLabel.font = UIFont.systemFont(ofSize: 15)
        scrollView.addSubview(equipsuggestLabel)
        
        let telLabel = UILabel(frame: CGRect(x: 15, y: equipsuggestLabel.frame.origin.y+30, width: UIScreen.main.bounds.width-30, height: 20))
        telLabel.text = "联系电话: \(infoDict.value(forKey: "club_tel") as! String)"
        telLabel.font = UIFont.systemFont(ofSize: 15)
        scrollView.addSubview(telLabel)
        
        let addrLabel = UILabel(frame: CGRect(x: 15, y: telLabel.frame.origin.y+30, width: UIScreen.main.bounds.width-30, height: 20))
        let addr = (infoDict.value(forKey: "province_name") as! String)+(infoDict.value(forKey: "city_name") as! String)+(infoDict.value(forKey: "district_name") as! String)
        addrLabel.text = "具体地址: \(addr)"
        addrLabel.font = UIFont.systemFont(ofSize: 15)
        scrollView.addSubview(addrLabel)
        
        let label3 = UILabel(frame: CGRect(x: 15, y: addrLabel.frame.origin.y+30, width: UIScreen.main.bounds.width-30, height: 35))
        label3.text = "相关视频"
        label3.textAlignment = .center
        label3.layer.cornerRadius = 5
        label3.layer.borderColor = UIColor.lightGray.cgColor
        label3.layer.borderWidth = 1
        scrollView.addSubview(label3)
        
        videoPlayer = VideoPlayer(frame: CGRect(x: 0, y: label3.frame.origin.y+40, width: WIDTH, height: 85))
        videoPlayer.initialize(itemUrl:"http://172.24.10.175/workout/Uploads/tvideo/video/zjs/mvc1.mp4")
        view.addSubview(videoPlayer)
        
        videoPlayer.fullBtn.addTarget(self, action: #selector(fullScreen), for: .touchUpInside)
        
        let joinBtn = CustomButton(frame: CGRect(x: 70, y: imageView.frame.origin.y+158, width: 90, height: 35))
        joinBtn.backgroundColor = UIColor.blue
        joinBtn.center.x = 320
        joinBtn.setTitle("关注", for: .normal)
        joinBtn.layer.cornerRadius = 3
        joinBtn.adjustsImageWhenDisabled = false
        joinBtn.isEnabled = false
        joinBtn.animation()
        scrollView.addSubview(joinBtn)
        
        isExits(sender: joinBtn)
        // self.tableView.reloadData()
        joinBtn.addTarget(self, action: #selector(joinClub), for: .touchUpInside)
        
        navBar.backBtn.addTarget(self, action: #selector(back), for: .touchUpInside)
        
    }
    
    func isExits(sender:CustomButton){
        let parameters:NSDictionary = [
     //       "SessionID"     :   LoginModel.sessionID,
     //       "fan_id"        :   infoDict.value(forKey: "id") as! String,
     //       "userid"        :   LoginModel.userID,
            "fans_type"     :   "1",
            ]
        YJNetworkMngTool.sharedNetWorkMngTool.isExits(parameter: parameters) {(success) in
            DispatchQueue.main.async {
                if success{
                    sender.setTitle("取消关注", for: .normal)
                }
                sender.stopAnimation()
                sender.isEnabled = true
            }
        }
    }
    
    @objc func joinClub(sender:UIButton){
        let parameter:NSDictionary = [
   //         "SessionID" :   LoginModel.sessionID,
   //         "fan_id"    :   infoDict.value(forKey: "id") as! String,
            "fans_type" :   "1",
  //          "userid"    :   LoginModel.userID
        ]
        if sender.isEnabled{
            let popup = Popup()
            if sender.titleLabel?.text == "关注"{
                YJNetworkMngTool.sharedNetWorkMngTool.YJNetwork_focus_Join(parameter: parameter) {
                    DispatchQueue.main.async {
                        popup.appear(title: "关注成功", action: {
                            self.back()
                        })
                    }
                }
            }else{
                YJNetworkMngTool.sharedNetWorkMngTool.YJNetwork_delete(parameter: parameter, block: {
                    DispatchQueue.main.async {
                        popup.appear(title: "取消关注成功", action: {
                            self.back()
                        })
                    }
                })
            }
        }
    }
    
    @objc func fullScreen(){
        let pw = videoPlayer.bounds.width
        let ph = videoPlayer.bounds.height
        if !hideState{
            UIView.animate(withDuration: 0.3, animations: {
                self.videoPlayer.transform =
                    CGAffineTransform(rotationAngle: CGFloat(Double.pi/180)*CGFloat(90)).concatenating(
                        CGAffineTransform(scaleX: self.WIDTH/ph,
                                          y: self.HEIGHT/pw)).concatenating(
                                            CGAffineTransform(translationX: 0,
                                                              y: -25.833-64))
            })
            hideState = !hideState
            setNeedsStatusBarAppearanceUpdate()
        }
        else{
            UIView.animate(withDuration: 0.3, animations: {
                self.videoPlayer.transform = .identity
            })
            hideState = !hideState
            setNeedsStatusBarAppearanceUpdate()
        }
    }
    
    override var prefersStatusBarHidden: Bool{
        return hideState
    }
    
    @objc func back(){
        videoPlayer.disAppear()
        _ = navigationController?.popViewController(animated: true)
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}



